/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;
using FileSystemDatabase;
using TomatoSynchronizer.Entities ;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of ProductVariantGroupStore.
	/// </summary>
	public class ProductVariantGroupStore : IStore<ProductVariantGroup>
	{
		private Site _site;
		public Site site {
			get { return _site; }
			set { _site = value; }
		}
		private HtmlWeb _client;
		
		private JsonDatabase db;
		public void PersistItems(List<ProductVariantGroup> items)
		{
			db.Write<List<ProductVariantGroup>>(items,_site.Name);
		}
		
		public void Truncate()
		{
			throw new NotImplementedException();
		}
		
		public void PersistItem(ProductVariantGroup item)
		{
			db.Write<ProductVariantGroup>(item,item.products_variants_groups_id);
		}				
		
		public ProductVariantGroupStore(Site site,HtmlWeb client)
		{
			db = new JsonDatabase("sites/" + site.Name + "/ProductVariant");
			_site = site;
			_client = client;
		}		
		
		public List<ProductVariantGroup> GetItems()
		{
			List<ProductVariantGroup> records = new List<ProductVariantGroup>();
			string url = _site.Url + "/admin/json.php?action=list_product_variants&module=product_variants";
			
			HtmlDocument doc = _client.Load(url);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						ProductVariantGroup record = (ProductVariantGroup)recs[i];
						record.Entries = GetItemVariantGroupEntries(record.products_variants_groups_id);
						records.Add(record);												
					}									
				}
			}			
			
			return records;
		}
		
		private List<ProductVariantGroupEntry> GetItemVariantGroupEntries(string groupId)
		{
			List<ProductVariantGroupEntry> entries = new List<ProductVariantGroupEntry>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_product_variants_entries&module=product_variants&products_variants_groups_id=" + groupId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						ProductVariantGroupEntry record = (ProductVariantGroupEntry)recs[i];
						entries.Add(record);												
					}									
				}
			}			
			
			return entries;
		}		
		
		public bool MoveItem(string itemId, string folderId)
		{
			throw new NotImplementedException();
		}
		
		public bool DeleteItem(string itemId)
		{
			throw new NotImplementedException();
		}
		
		public ProductVariantGroup[] LoadItems()
		{
			throw new NotImplementedException();
		}
		
		public ProductVariantGroup LoadItem(string itemId)
		{
			throw new NotImplementedException();
		}

		public ProductVariantGroup GetItem(string itemId)
		{
			throw new NotImplementedException();
		}
		
		public bool Contains(ProductVariantGroup item)
		{
			throw new NotImplementedException();
		}		
		
		public bool UpdateItem(ProductVariantGroup item)
		{
			throw new NotImplementedException();
		}
		
		public string InsertItem(ProductVariantGroup item)
		{
			throw new NotImplementedException();
		}
	}
}
